<?php
class MyPDO {

    private $dbh;
    private $stmt;
    public static $instance = NULL;

    private function __construct($site = NULL) {
        if ($site == NULL) {
            $dbName = DB_NAME;
        } else {
            $dbName =  'choigame_'.$site;
        }
        $this -> dbh = new PDO("mysql:host=" . DB_HOST_NAME . ";dbname=" . $dbName, DB_USER, DB_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
        $this -> dbh -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this -> dbh -> setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        $this -> dbh -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        return $this -> dbh;
    }

    public static function getInstance($site = NULL) {
        
        self::$instance = new MyPDO($site);

        return self::$instance;
    }

    public function loadOneRow($inSQL, $param = array()) {
        $this -> stmt = $this -> dbh -> prepare($inSQL);
        $this -> stmt -> execute($param);
        $row = $this -> stmt -> fetch();
        return $row;
    }

    public function loadManyRow($inSQL, $param = array()) {
        $table = array();
        $this -> stmt = $this -> dbh -> prepare($inSQL);
        $this -> stmt -> execute($param);
        while ($row = $this -> stmt -> fetch()) {
            $table[] = $row;
        }
        return $table;
    }

    public function executeNonQuery($inSQL, $param = array()) {
        $this -> stmt = $this -> dbh -> prepare($inSQL);
        $result = $this -> stmt -> execute($param);
        return $result;
    }

}
?>